<?php
/**
* 
*/
class Login_model extends Model
{
	
	function __construct()
	{
		parent::__construct();
	}

	public function login()
	{
		$pass = md5($_POST['password']);
		$q = $this->db->prepare("SELECT User_Id,role,Avatar,FirstName FROM users WHERE Email = :user_name AND Password = :password");
		$q->execute(array(
			':user_name' 	=> 	$_POST['user_name'],
			':password'		=>	$pass
		));
		// return array
		//$data = $q->fetchAll();
		$data = $q->fetch();
		$return = array('success' => false);
		if(!empty($data)){
			Session::init();
			Session::set('loggerIn',true);
			$_SESSION["id"] = $data['User_Id'];
			$_SESSION["FirstName"] = $data['FirstName'];
			if($data['Avatar'] != null) $_SESSION["img"] = URL."public/img/".$data['Avatar'];
			else $_SESSION["img"] = URL."public/img/coffee.png";
			$return['success'] = true;
		}		

		echo json_encode($return);
	}
}
?>